from rest_framework import serializers
from lapi_app.models.testcase_model.testcase_tree import Menu


class OrganizeTableModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Menu
        fields = ["id", "label", 'pid', 'type']

    def xTree(data):
        # 递归处理数据
        lists = []
        tree = {}
        for i in data:
            item = i
            tree[item['id']] = item

        for i in data:
            obj = i
            if not obj['pid']:
                root = tree[obj['id']]
                lists.append(root)
            else:
                parent_id = obj['pid']
                if 'children' not in tree[parent_id]:
                    tree[parent_id]['children'] = []
                tree[parent_id]['children'].append(tree[obj['id']])
        return lists
